package com.hongshu.idle.service.idle.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongshu.common.core.enums.DealStatusEnum;
import com.hongshu.common.core.enums.PayStatusEnum;
import com.hongshu.common.core.enums.PayTypeEnum;
import com.hongshu.common.core.enums.ResultCodeEnum;
import com.hongshu.common.core.exception.HongshuException;
import com.hongshu.idle.auth.AuthContextHolder;
import com.hongshu.idle.domain.entity.IdlePaymentOrder;
import com.hongshu.idle.domain.entity.IdleProductOrder;
import com.hongshu.idle.domain.entity.IdleUser;
import com.hongshu.idle.mapper.idle.IdlePaymentOrderMapper;
import com.hongshu.idle.mapper.idle.IdleProductOrderMapper;
import com.hongshu.idle.mapper.idle.IdleUserMapper;
import com.hongshu.idle.service.idle.IIdlePaymentOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品
 *
 * @author: hongshu
 */
@Slf4j
@Service
public class IdlePaymentOrderServiceImpl extends ServiceImpl<IdlePaymentOrderMapper, IdlePaymentOrder> implements IIdlePaymentOrderService {

    @Autowired
    private IdleUserMapper userMapper;
    @Autowired
    private IdleProductOrderMapper orderMapper;
    @Autowired
    private IdlePaymentOrderMapper paymentMapper;


    /**
     * 创建订单
     *
     * @param productOrderId 商品订单ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createPaymentOrder(String productOrderId) {
//        String userId = WebUtils.getRequestHeader(UserConstant.USER_ID);
        String userId = AuthContextHolder.getUserId();
        IdleUser user = userMapper.selectById(userId);

        IdleProductOrder productOrder = orderMapper.selectById(productOrderId);
        if (BeanUtil.isEmpty(productOrder)) {
            throw new HongshuException(ResultCodeEnum.FAIL);
        }
        if (!DealStatusEnum.TEMPORARY.getCode().equals(productOrder.getDealStatus())) {
            throw new HongshuException(ResultCodeEnum.FAIL);
        }
        IdlePaymentOrder paymentOrder = new IdlePaymentOrder();
        paymentOrder.setOrderNumber(RandomUtil.randomNumbers(12));
        paymentOrder.setProductId(productOrder.getProductId());
        paymentOrder.setUid(user.getId());
        paymentOrder.setPayType(PayTypeEnum.ALI_PAY.getCode());
        paymentOrder.setPayTypeName(PayTypeEnum.ALI_PAY.getDesc());
        paymentOrder.setPayPrice(Long.valueOf(productOrder.getAnswerBuyMoney()));
        paymentOrder.setOrderStatus(PayStatusEnum.UN_PAY.getCode());
        paymentOrder.setCreateTime(new DateTime());
        paymentOrder.setUpdateTime(new DateTime());
        paymentMapper.insert(paymentOrder);

        // 更新产品订单
        productOrder.setPaymentOrderId(paymentOrder.getId());
        productOrder.setOrderStatus(PayStatusEnum.PEND_PAY.getCode());
        orderMapper.updateById(productOrder);
        return paymentOrder.getId();
    }

    /**
     * 获取支付订单
     *
     * @param paymentOrderId 支付订单ID
     */
    @Override
    public Object getPaymentOrder(String paymentOrderId) {
        IdlePaymentOrder paymentOrder = paymentMapper.selectById(paymentOrderId);
        if (ObjectUtils.isEmpty(paymentOrder)) {
            throw new HongshuException(ResultCodeEnum.FAIL);
        }
        return paymentOrder;
    }
}
